<?php
namespace admin\action;

use api\utils\SaasApiClient;
use herosphp\bean\Beans;
use herosphp\core\Controller;
use herosphp\http\HttpRequest;
use herosphp\session\Session;
use herosphp\utils\AjaxResult;

/**
 * @author
 */
class QuoteDataAction extends CommonAction
{


    public function index(HttpRequest $request)
    {
        $this->page = $request->getParameter('page', 'intval');
        $this->pagesize = $request->getParameter('pagesize', 'intval');
        $starttime = $request->getParameter('starttime');
        $endtime = $request->getParameter('endtime');
        if ($this->page <= 0) $this->page = 1;
        if ($this->pagesize <= 0) $this->pagesize = 15;

        $tab = $request -> getParameter('tab');
        $conditions = "";
        if( $tab ){
            $conditions .= " and a.product_name = '{$tab}'";
        }
        if( $starttime ){
            $conditions .= " and a.addtime >= '".$starttime." 00:00:00'";
        }
        if( $endtime ){
            $conditions .= " and a.addtime <= '".$endtime." 23:59:59'";
        }
        $quoteDataService = Beans::get('quote.data.service');
        //$total = $quoteDataService -> count($conditions);

        $res = $quoteDataService -> getDataList($conditions,"addtime desc",array($this->page, $this->pagesize));

        if( $tab ) {
            $today = strtotime(date('Y-m-d') . ' 00:00:00');
            $yesterday = $today - 86400;
            //今天
            if (strtotime($res['items'][0]['addtime']) > $today) {
                $has_quote = "yes";
                $alertRes = $quoteDataService->getDataList(" and a.product_name = '{$tab}' and a.addtime >'" . date('Y-m-d', $today) . "'", "addtime,id desc");
            } else {
                //昨天
                $has_quote = "";
                $alertRes = $quoteDataService->getDataList(" and a.product_name = '{$tab}' and a.addtime >'" . date('Y-m-d', $yesterday) . "'", "addtime,id desc");
            }
        }
        $product_arr = array(
            'PVC' => [1,5],
            'PP' => [1,6],
            'PET' => [1,56],
            'LLDPE' => [1,4],
            'ABS' => [1,1],
            'HIPS' => [1,7],
            'HDPE' => [1,3],
            'PC' => [2,9],
            'GPPS' => [1,2]
        );

        $url = "api/brand/getFactorys";
        $result = SaasApiClient::send($url, array('typeId' => (string)$product_arr[$tab][0], 'productId' => (string)$product_arr[$tab][1]));
        if ($result->isSucess()) {

            $factory = $result->getData();
        } else {
            $factory=[];
        }


        $this->generatePageMenu($res['total']);
        $this->assign('items',$res['items']);
        $this->assign('starttime',$starttime);
        $this->assign('endtime',$endtime);
        $this->assign('tab',$tab);
        $this->assign('sel_value',$product_arr[$tab][1]);
        $this->assign('type_id',$product_arr[$tab][0]);
        $this->assign('factory',$factory);
        $this->assign('has_quote',$has_quote);
        $this->assign('alertRes',$alertRes['items']);
        $this->setView('quoteData/index');
        $this->assign('breadcrumb', array('行情管理', '行情数据列表'));

    }

    public function insert(HttpRequest $request){

        $user_id = $this->loginUser->getId();
        //新增的数据
        $data = $request->getParameter('data');
        $has_quote = $request->getParameter('has_quote');
        $new_data = [];
        if( is_array($data) ) {
            $count = count($data['product_name_new']);
            for($i=0;$i<$count;$i++){
                $new_data[$i]['pfb'] = $data['product_name_new'][$i].'-'.$data['factory_name_new'][$i].'-'.$data['brand_name_new'][$i];
                //$new_data[$i]['factory_name'] = $data['factory_name_new'][$i];
                //$new_data[$i]['brand_name'] = $data['brand_name_new'][$i];

                $new_data[$i]['product_name'] = $data['product_name_new_text'][$i];
                $new_data[$i]['factory_name'] = $data['factory_name_new_text'][$i];
                $new_data[$i]['brand_name'] = $data['brand_name_new_text'][$i];

                //$new_data[$i]['yesterday_new'] = $data['yesterday_new'][$i];
                $new_data[$i]['price'] = $data['today_new'][$i];
                $new_data[$i]['manager_id'] = $user_id;
            }
        }

        //原来的数据
        $today_id = $request->getParameter('today_id');
        $today = $request->getParameter('today');
        $pfb = $request->getParameter('pfb');
        $today_data = [];
        if( is_array($today_id) ){
            $count = count($today_id);
            for( $j=0; $j<$count; $j++ ){
                $today_data[$j]['id'] = $today_id[$j];
                $today_data[$j]['price'] = $today[$j];
                $today_data[$j]['pfb'] = $pfb[$j];
                $today_data[$j]['manager_id'] = $user_id;
            }
        }

        $quote_data = [
            'new' => $new_data,
            'copy' => $today_data,
            'has_quote' => $has_quote
        ];
        $quoteDataService = Beans::get('quote.data.service');
        if( $quoteDataService -> addOrUpdateQuoteData($quote_data) ){
            AjaxResult::ajaxSuccessResult();
        }else{
            AjaxResult::ajaxFailtureResult();
        }
    }
    public function softDelete(HttpRequest $request){
        $id = $request->getParameter('id', 'trim');
        if ( empty($id) ) AjaxResult::ajaxFailtureResult();
        $quoteDataService = Beans::get('quote.data.service');
        if( $quoteDataService -> update(['is_delete'=>1],$id) ){
            $request->addParameter('logmessage', "删除行情数据 [id:$id]");
            AjaxResult::ajaxSuccessResult();
        }else{

            AjaxResult::ajaxFailtureResult();
        }

    }



}
